package cn.tedu.tmall.server.config;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

@Configuration
public class AMQPConfig {
    //消息队列IP地址
    @Value("${amqp.host}")
    private String mqHost;

    @Value("${amqp.port}")
    private String mqPort;

    @Value("${amqp.username}")
    private String mqUsername;

    @Value("${amqp.password}")
    private String mqPassword;

    @Value("${amqp.virtual_host}")
    private String virtaulHost;


    @Bean
    public Channel getChannel() throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(mqHost);
        factory.setPort(Integer.parseInt(mqPort));
        factory.setUsername(mqUsername);
        factory.setPassword(mqPassword);
        factory.setVirtualHost(virtaulHost);
        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();
        return channel;
    }
}
